﻿Imports iTunesLib
Imports TwitterVB2
Imports Microsoft.Win32
Imports System.Web
Public Class Form
    Dim iEvent As _IiTunesEvents
    Dim m_iTunes As New iTunesLib.iTunesApp
    Dim tw As New TwitterAPI
    Public currentsong As String
    Public OAuthToken As String
    Public OAuthTokenSecret As String
    Public TwitterConsumerKey As String = "ENTER YOUR TWITTER CONSUMER KEY"
    Public TwitterConsumerSecret As String = "ENTER YOUR TWITTER CONSUMER SECRET"
    Delegate Sub SetLabelText_Delegate(ByVal [Label] As Label, ByVal [text] As String)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadform()
    End Sub
    Private Sub loadform()
        AddHandler m_iTunes.OnPlayerPlayEvent, AddressOf EventHandler
        Dim registryKey As RegistryKey
        registryKey = Registry.LocalMachine
        Dim registrySubKey As RegistryKey
        registrySubKey = registryKey.OpenSubKey("SOFTWARE\OpenSource\mytunes", False)
        If registrySubKey Is Nothing Then
            validatetwitteraccount()
        Else
            Label1.Visible = True
            Label2.Visible = False
            Button1.Visible = False
            TextBox1.Visible = False
            OAuthToken = registrySubKey.GetValue("OAuthToken")
            OAuthTokenSecret = registrySubKey.GetValue("OAuthTokenSecret")
            currentsong = "Listening to " & m_iTunes.CurrentTrack.Name & " by " & m_iTunes.CurrentTrack.Artist
            SetLabelText_ThreadSafe(Me.Label1, currentsong)
            Tweet(currentsong)
        End If
    End Sub
    Private Sub EventHandler(ByVal iTrack As Object)
        currentsong = "Listening to " & iTrack.Name & " by " & m_iTunes.CurrentTrack.Artist
        SetLabelText_ThreadSafe(Me.Label1, currentsong)
        Tweet(currentsong)
    End Sub
    Private Sub validatetwitteraccount()
        Label1.Visible = False
        Label2.Visible = True
        Button1.Visible = True
        Dim Url As String = tw.GetAuthorizationLink(TwitterConsumerKey.ToString, TwitterConsumerSecret.ToString)
        Process.Start(Url)
    End Sub
    Private Sub Tweet(ByVal currentsong As String)
        Dim tw As New TwitterAPI
        tw.AuthenticateWith(TwitterConsumerKey.ToString, TwitterConsumerSecret.ToString, OAuthToken.ToString, OAuthTokenSecret.ToString)
        tw.Update(currentsong)
    End Sub
    Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Label, ByVal [text] As String)
        If [Label].InvokeRequired Then
            Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
            Me.Invoke(MyDelegate, New Object() {[Label], [text]})
        Else
            [Label].Text = [text]
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim IsValid As Boolean = tw.ValidatePIN(TextBox1.Text)
        If IsValid Then
            OAuthToken = tw.OAuth_Token()
            OAuthTokenSecret = tw.OAuth_TokenSecret()
            Dim registryKey As RegistryKey
            registryKey = Registry.LocalMachine
            Dim registrySubKey As RegistryKey
            registrySubKey = registryKey.CreateSubKey("SOFTWARE\OpenSource\mytunes")
            registrySubKey = registryKey.OpenSubKey("SOFTWARE\OpenSource\mytunes", True)
            registrySubKey = registryKey.CreateSubKey("SOFTWARE\OpenSource\mytunes")
            registrySubKey.SetValue("oauthtoken", OAuthToken)
            registrySubKey.SetValue("OAuthTokenSecret", OAuthTokenSecret)
            loadform()
        End If
    End Sub
End Class
